package com.cutthroatcreations.woodenrailway;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;

public class YearListActivity extends FragmentActivity implements
		YearListFragment.OnYearSelectedListener,
		YearListFragment.setTextViewYear,
		TrainListFragment.OnTrainSelectedListener,
		TrainListFragment.OnBackListener,
		TrainListFragment.OnUpdateListener,
		SingleTrainFragment.OnImageSelectedListener,
		SingleTrainFragment.UpdateOwnedListener{

	private static final String TAG = "YearListActivity";
	public int index = 0;
	public ContentWorker cw;
	public TextView tv = null;
	public Fragment trainFragment;
	EditText inputSearch;
	FragmentManager man;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		cw = new ContentWorker(this);
		trainFragment = null;
		Fragment yearFragment = new YearListFragment();
		setContentView(R.layout.trainlist_fragment);
		inputSearch = (EditText) findViewById(R.id.inputSearch);
		inputSearch.setEnabled(false);
		man = getSupportFragmentManager();
		FragmentTransaction ft = man.beginTransaction();
		ft.add(R.id.fragFrame, yearFragment);
		ft.commit();
}
	

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();

		if (WoodenApplication.allTrains != null && trainFragment == null){
			inputSearch.setEnabled(false);
			cw.allOwned();
			runSetText();
		}

	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		
	}	

	@Override
	public void onYearSelected(int index) {
		WoodenApplication.setYearIndex(index);

		trainFragment = new TrainListFragment();
		FragmentTransaction ft = man.beginTransaction();
		ft.replace(R.id.fragFrame, trainFragment, "trainFrag");
		ft.addToBackStack(null);
		ft.commit();
		cw.yearOwned(index);
		runSetText();
		inputSearch.setEnabled(true);
		/**
		 * Enabling Search Filter
		 * */
		inputSearch.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence cs, int arg1, int arg2,
					int arg3) {

				// When user changed the Text
				TrainListFragment fragment = (TrainListFragment) getSupportFragmentManager().findFragmentByTag("trainFrag");
				fragment.searchIt(cs);

			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub
			}
		});

	}

	@Override
	public void onTrainSelected(int id, TrainListAdapter tla) {
		SingleTrainFragment viewer = (SingleTrainFragment) getSupportFragmentManager()
				.findFragmentById(R.id.singletrain_fragment);
		if (viewer == null || !viewer.isInLayout()) {
			Log.i(TAG, "notInLayout");
			Intent intent = new Intent(WoodenApplication.getContext(),
					SingleTrainActivity.class);
			intent.putExtra("train", id);

			startActivity(intent);
		} else {

			viewer.updateTrain(getIndexById(id), tla);
		}

	}

	@Override
	public void onImageSelected(int id) {
		Log.i(TAG, "notInLayout");
		Intent intent = new Intent(WoodenApplication.getContext(),
				LargeImageActivity.class);
		intent.putExtra("index", getIndexById(id));
		startActivity(intent);

	}

	public int getIndexById(int trainId) {

		for (int i = 0; i < WoodenApplication.getAllTrains().size(); i++) {
			if (WoodenApplication.getAllTrains().get(i).getID() == trainId) {
				return i;
			}
		}
		return -1;
	}
	
	@Override
	public void updateTextView(int index) {
		cw.yearOwned(index);
		runSetText();
	}

	@Override
	public void setTextView() {
		Log.i(TAG, "start setTextView()");
		inputSearch.setEnabled(false);
		cw.allOwned();
		runSetText();
	}

	private void runSetText() {
		if (tv == null)
			tv = (TextView) findViewById(R.id.amountOwned);
		tv.setText("You own " + String.format("%.2f", cw.getOwned())
				+ "% of the total Collection.");
	}


	

}
